package ninja.awesome.game.gameobject;

import java.util.Random;

import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.compat.Rectangle;
import ninja.awesome.game.main.Animation;
import ninja.awesome.game.main.Controller;
import ninja.awesome.game.main.Sprites;

public class Tree extends GameObj {

	private Image[] tree;
	private int speed = 5;

	private int ticks = 0;
	private int timer = 0;

	private int imageNumber = 1;
	private double imageWidth = 0;
	private double imageHeight = 0;
	private int whatTree = 0;
	int level = 0;
	private int left = 0;
	private int right = 1;
	private boolean buildingCollide = false;

	private String direction = "left";
	Animation anim;
	Random r = new Random();
	MainGameScreen game;
	Sprites s;
	Controller c;

	public Tree(int x, int y, MainGameScreen game, Controller c, Sprites s) {

		super(x, y);
		this.s = s;
		this.c = c;
		this.game = game;
		whatTree = r.nextInt(4);
		tree = s.getTree(whatTree);

		if (whatTree == 0) { //  green tree 01
			this.imageWidth = 181;
			this.imageHeight = 150;
			yLoc+= 82;

		} else if (whatTree == 1) { // green tree 02

			this.imageWidth = 200;
			this.imageHeight = 157;
			yLoc +=79;

		}else if (whatTree == 2) { // pink tree

			this.imageWidth = 261;
			this.imageHeight = 236;
			yLoc -=5;

		}else if (whatTree == 3){// cut tree
			this.imageWidth = 218;
			this.imageHeight = 69;
			yLoc +=160;


		}
		anim = new Animation(0, tree[0], imageHeight, imageWidth,
				imageNumber);

	}

	public void update() {
		if (game.getBuilding_Block()) {
			right = 0; // to make road not movable! going right
		} else
			right = 1;

		if (game.getNinja().getDirection().equals("left")) {
			xLoc -= game.getNinja().getSpeedX() * 1;
			if (xLoc > 0) {
				if (c.getTree1().size() < 2 || c.getTree2().size() <2)
					c.spawnTree(-1);
				else if (xLoc > 2000)
					c.removeTree1(this);

			}
		} else if (game.getNinja().getDirection().equals("right")) {
			xLoc -= game.getNinja().getSpeedX() * right;
			if (xLoc < 0) {
				if (c.getTree1().size()<2 ||c.getTree2().size() < 2 )
					c.spawnTree(1);
				else if (xLoc < -2000)
					c.removeTree1(this);
			}
		}

	}

	public void draw(Graphics g) {
		anim.drawAnimation(g, xLoc, yLoc);
	}

	public int getX() {
		return xLoc;
	}

	public int getY() {
		return yLoc;
	}

	public void SetX(int x) {
		this.xLoc = x;
	}

	public void SetY(int y) {
		this.yLoc = y;
	}

	public Rectangle getBounds(){
		return new Rectangle(xLoc, yLoc, (int)imageWidth, (int)imageHeight);
	}

}
